package com.school.parentServlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.school.parentServlet.cmd.ParentCommand;
import com.school.parentServlet.cmd.ParentHomeCommand;
import com.school.parentServlet.cmd.ViewMyChildAssignmentCommand;
import com.school.parentServlet.cmd.ViewMyChildAttendanceCommand;
import com.school.parentServlet.cmd.ViewMyChildExamDateSheetCommand;
import com.school.parentServlet.cmd.ViewMyChildFeesDetailsCommand;
import com.school.parentServlet.cmd.ViewMyChildProfileCommand;
import com.school.parentServlet.cmd.ViewMyChildReportCardCommand;
import com.school.parentServlet.cmd.ViewMyChildTimeTableCommand;




/**
 * This servlet will act as parent controller which will accept request from and send response to parent only.
 * Servlet implementation class ParentController
 * @author KK
 */

	public class ParentController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static Logger log = Logger.getLogger(ParentController.class.getSimpleName());
	   
	private ServletConfig config;
	private Map<String, ParentCommand> parentCommands;
	
	/**
	 * 
	 */
	public void init(ServletConfig config) {
		log.info("Entered init(ServletConfig)");
		this.config = config;
		log.info("Cached the ServletConfig object");
		
		parentCommands = new HashMap<String, ParentCommand>();
		parentCommands.put("parentHome", new ParentHomeCommand());
		parentCommands.put("viewStudProfile", new ViewMyChildProfileCommand());
		parentCommands.put("viewStudTimeTable", new ViewMyChildTimeTableCommand());
		parentCommands.put("viewStudAttendance", new ViewMyChildAttendanceCommand());
		parentCommands.put("viewStudAssignment", new ViewMyChildAssignmentCommand());
		parentCommands.put("viewStudReportCard", new ViewMyChildReportCardCommand());
		parentCommands.put("viewStudFeesDetails", new ViewMyChildFeesDetailsCommand());
		parentCommands.put("viewExamDateSheet", new ViewMyChildExamDateSheetCommand());
		
		
		
		
		config.getServletContext().setAttribute("parentCommands", parentCommands);
		log.info("Exited init(ServletConfig)");
		
	}
	
	/**
	 * 
	 */
	public void init() {
		log.info("Entered init()");
		log.info("Exited init()");
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {
		processRequest(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		processRequest(req, resp);
	}

	private void processRequest(HttpServletRequest req, HttpServletResponse resp) 
	throws ServletException, IOException {
		HttpSession session = req.getSession();
		String parentCommand = req.getParameter("cmd");
		
		System.out.println("Command received is " + parentCommand);
		if (parentCommands.get(parentCommand) == null) {
			parentCommand = "parentHome";
		}
		
		if(session.getAttribute("type")== "parent"){
			parentCommands.get(parentCommand).processRequest(req, resp, config);
		}
		else {
			System.out.println("Acting very smart?? :-):-)!! Relogin to continue!");
		}
		
		
		
	}
	

}
